<template>
    <div>
        <h1 style="text-align: center;padding: 10px 0 30px 0">品牌列表</h1>
        <el-breadcrumb separator=">" style="font-size: 16px">
            <el-breadcrumb-item :to="{ path: '/' }">
                <i class="el-icon-s-promotion"></i>后台管理
            </el-breadcrumb-item>
            <el-breadcrumb-item>品牌列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider></el-divider>

        <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="id" label="ID" width="50" align="center"></el-table-column>
            <el-table-column prop="name" label="名称" width="200" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="pinyin" label="拼音" width="200" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="logo" label="logo" width="200" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="description" label="简介" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="keywords" label="关键词列表" width="100" align="center"></el-table-column>
            <el-table-column prop="sort" label="排序序号" width="100" align="center"></el-table-column>
            <el-table-column label="是否启用" width="80" align="center">
                <template slot-scope="scope">
                    <el-switch
                            @change="toggleEnable(scope.row)"
                            v-model="scope.row.enable"
                            :active-value="1"
                            :inactive-value="0"
                            active-color="#13ce66"
                            inactive-color="#cccccc">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="100" align="center">
                <template slot-scope="scope">
                    <el-button type="primary"
                               icon="el-icon-edit"
                               circle size="mini"
                               style="background-color: #428fd4;
                               border-color: #428fd4"
                               @click="openEditDialog(scope.row)"></el-button>
                    <el-button type="danger"
                               icon="el-icon-delete"
                               circle size="mini"
                               @click="openDeleteConfirm(scope.row)"></el-button>
                </template>
            </el-table-column>
        </el-table>

    </div>
</template>

<script>
    export default {
        data() {
            return {
                tableData: [],
                dialogFormVisible: false,
                form: {
                    id:'',
                    name: '',
                    pinyin: '',
                    description:'',
                    keywords:'',
                    sort:'',
                },
                formLabelWidth: '120px'
            }
        },
        methods: {

            /* 开启或关闭控件 */
            toggleEnable(admin) {
                console.log('你点击了【' + admin.username
                    + '】的开关控件，当前开关值：' + admin.enable);
                let enableText = ['禁用', '启用'];
                let url = 'http://localhost:9080/brands/' + admin.id;
                if (admin.enable == 1) {
                    url += '/enable';
                } else {
                    url += '/disable';
                }
                console.log('url = ' + url);

                this.axios.post(url).then((response) => {
                    let responseBody = response.data;
                    if (responseBody.state == 20000) {
                        let message = '将品牌【' + admin.name + '】的启用状态修改为【'
                            + enableText[admin.enable] + '】成功！';
                        this.$message({
                            message: message,
                            type: 'success'
                        });
                    } else {
                        this.$message.error(responseBody.message);
                    }
                    if (responseBody.state == 40400) {
                        this.loadAdminList();
                    }
                });
            },

            /*删除数据前的确认弹窗*/
            openDeleteConfirm(album) {
                let message = '此操作将永久删除【' + album.name + '】品牌，是否继续？';
                this.$confirm(message, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.handleDelete(album);
                }).catch(() => {
                });
            },

            /*根据id删除某条数据的请求*/
            handleDelete(album) {
                let url = 'http://localhost:9080/brands/' + album.id + '/delete';
                console.log('url = ' + url);
                let formData = 'id=' + album.id;
                console.log('formData = ' + formData);

                this.axios.post(url, formData).then((response) => {
                    let responseBody = response.data;
                    if (responseBody.state == 20000) {
                        this.loadAlbumList();
                    } else {
                        this.$alert(responseBody.message, '操作失败', {
                            confirmButtonText: '老子知道了',
                            callback: action => {
                                this.loadAlbumList();
                            }
                        });
                    }
                });
            },

            /*查询集合列表的请求*/
            loadAlbumList() {
                console.log("打个桩看看吧: loadAlbumList..........")
                let url = 'http://localhost:9080/brands';
                console.log("url=" + url);
                this.axios.get(url).then((response) => {
                    let responseBody = response.data;
                    this.tableData = responseBody.data;
                })
            },
        },
        created() {},
        mounted() {
            console.log("打个桩看看吧: mounted..........")

            /*查询集合列表的请求*/
            this.loadAlbumList();
        }
    }
</script>

<style scoped>

</style>